package com.infore.remoteControl.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.infore.remoteControl.constant.ResultConstants;
import com.infore.remoteControl.dto.InstructionsDto;
import com.infore.remoteControl.entity.instructionsEntity.Instructions;
import com.infore.remoteControl.entity.instructionsEntity.InstructionsData;
import com.infore.remoteControl.enums.InstructionsStateEnum;
import com.infore.remoteControl.enums.ResultStatus;
import com.infore.remoteControl.enums.StrategyCodeEnum;
import com.infore.remoteControl.repository.IInstructionDataRepository;
import com.infore.remoteControl.repository.IInstructionRepository;
import com.infore.remoteControl.service.IScheduleService;
import com.infore.remoteControl.service.InstructionsService;
import com.infore.remoteControl.util.MyBeanUtils;
import com.infore.remoteControl.util.ResultEntity;

@Service
public class ScheduleServiceImpl implements IScheduleService {
	
	@Autowired
	InstructionsService instructionsService;
	
	@Autowired
	IInstructionRepository iInstructionRepository;
	
	@Autowired
	IInstructionDataRepository iInstructionDataRepository;
	
	@Autowired
	RealTimeSendStrategy RealTimeSendStrategy;
	
	@Autowired
	TimerSendStrategy TimerSendStrategy;
	
	@Autowired
	DelayedSendStrategy DelayedSendStrategy;
	
	@Override
	public ResultEntity schedule(List<InstructionsDto> instructionsDtoList) {
		//1.生成指令数据（调用指令管理相应接口）；
		instructionsService.createInstructionsDatas(instructionsDtoList);
		//2.调用相应策略发送指令，接收返回消息；
		for (InstructionsDto instructionsDto : instructionsDtoList) {
			String instructionsId = instructionsDto.getInstructionsId();
			String equipmentId = instructionsDto.getEquipmentId();
			String strategyCode = instructionsDto.getStrategyCode();
			// 将所传的参数保存至指令数据内
			Instructions instructions = new Instructions(); // 指令实体类
			instructions.setInstructionsId(instructionsId);
			InstructionsData instructionsData = new InstructionsData(); // 指令数据实体类
			instructionsData.setInstructions(instructions);
			instructionsData.setEquipmentId(equipmentId);
			instructionsData.setSendstrategyCode(strategyCode);
			// 根据策略编号选择发送方式
			if (StrategyCodeEnum.REAL.getCode().equals(strategyCode)) {
				// 实时发送
				RealTimeSendStrategy.send(instructionsData);
			} else if (StrategyCodeEnum.TIMER.getCode().equals(strategyCode)) {
				// 定时发送
				TimerSendStrategy.send(instructionsData);
			} else if (StrategyCodeEnum.DELAYED.getCode().equals(strategyCode)) {
				// 延时发送
				DelayedSendStrategy.send(instructionsData);
			}
			//3.更新指令数据（调用指令管理相应接口）
			if (StringUtils.isNotBlank(instructionsData.getEquipmentId())) {
				//查询原有的对象
				InstructionsData oldInstructionData = iInstructionDataRepository.findByEquipmentId(instructionsData.getEquipmentId());
				//判断对象是否存在
				if (oldInstructionData != null) {
					MyBeanUtils.copyNonNullProperties(instructionsData,oldInstructionData);
					instructionsData = oldInstructionData;
				} else {
					return new ResultEntity(ResultStatus.ERROR, ResultConstants.NO_SUCH_OBJECT);
				}
			}
			instructionsData.setState(InstructionsStateEnum.SENT.getCode()); // 已发送状态
			instructionsData.setContent("444444"); // 指令内容
			iInstructionDataRepository.saveAndFlush(instructionsData);
		}
		return null;
	}

}
